#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <unordered_map>
using namespace std;
typedef long long ll;
int main() {
    string s;
    while (cin >> s) {
        ll sum = 0;
        unordered_map<char, int> mp;
        priority_queue<int, vector<int>, greater<int>> q;
        for (auto& e : s) {
            mp[e]++;
        }
        for (auto& e : mp) {
            q.push(e.second);
        }
        while (q.size() > 1) {
            int t1 = q.top();
            q.pop();
            int t2 = q.top();
            q.pop();
            sum += (t1 + t2);
            q.push(t1 + t2);
        }
        cout << sum << endl;
    }
    return 0;
}